package com.example.textroom;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.textroom.app.UserDatabase;
import com.example.textroom.bead.UserTo;
import com.example.textroom.bead.Word;
import com.example.textroom.data.dao.WordDao;
import com.example.textroom.data.repository.WordRepository;
import com.example.textroom.data.repository.viewmodel.WordViewModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    WordViewModel wordViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        Button button = findViewById(R.id.bt_view);
        MyAdapter myAdapter = new MyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);

        // ViewModelProviders 需要添加依赖，而不是直接导入
        wordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

        // 观察者模式: wordViewModel不再有observe方法，可以使用LiveData的observe方法来替代。
        wordViewModel.getAllWordsLive().observe(this, words -> {
            myAdapter.setAllWords(words);
            myAdapter.notifyDataSetChanged();
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WordRepository userRepository=new WordRepository(MainActivity.this.getApplicationContext());
                Word word=new Word("111","222");
                userRepository.insertWords(word);
                Intent i=new Intent(MainActivity.this,TextActivity.class);
                startActivity(i);
            }
        });


        new Thread(new Runnable() {
            @Override
            public void run() {
                UserDatabase userDatabase= UserDatabase.getDatabase(MainActivity.this.getApplicationContext());
                UserTo userTo =new UserTo("nihao",1,12123123L);
                userDatabase.getUserDao().insertUser(userTo);
                List<UserTo> allUserTos = userDatabase.getUserDao().getAll();
                Log.e("YYYYYY","++"+ allUserTos.size());
                List<UserTo> userTos = userDatabase.getUserDao().searchUsersSync(1, "i", null, null);
                Log.e("YYYYYY","++@@@"+ userTos.size());
                WordDao wordDao = userDatabase.getWordDao();
            }
        }).start();

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);

    }
}